<!-- BI 排行版 -->
<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
      label-position="left"
    >
      <el-form-item label="时间范围" prop="orderDate">
        <el-date-picker
          :clearable="false"
          v-model="queryParams.times"
          :shortcuts="defaultShortcuts"
          class="!w-240px"
          end-placeholder="结束日期"
          start-placeholder="开始日期"
          type="daterange"
          value-format="YYYY-MM-DD HH:mm:ss"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
        />
      </el-form-item>
      <el-form-item label="归属部门" prop="deptId">
        <el-tree-select
          v-model="queryParams.deptId"
          :data="deptList"
          :props="defaultProps"
          check-strictly
          node-key="id"
          placeholder="请选择归属部门"
        />
      </el-form-item>
    </el-form>
    <div class="flex justify-between">
      <div>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </div>
    </div>
  </ContentWrap>

  <!-- 排行数据 -->
  <el-col>
    <el-tabs v-model="activeTab">
      <!-- 外呼总量排行  -->
      <el-tab-pane label="外呼总量排行" name="callRank" lazy>
        <CallRank :query-params="queryParams" ref="callRankRef" />
      </el-tab-pane>
      <!-- 接通数量排行 -->
      <el-tab-pane label="接通数量排行" name="connectRank" lazy>
        <ConnectRank :query-params="queryParams" ref="connectRankRef" />
      </el-tab-pane>
      <!-- 通话时长排行 -->
      <el-tab-pane label="通话时长排行" name="durationRank" lazy>
        <DurationRank :query-params="queryParams" ref="durationRankRef" />
      </el-tab-pane>
      <!-- 外呼渠道排行 -->
      <el-tab-pane label="外呼方式排行" name="channelRank" lazy>
        <ChannelRank :query-params="queryParams" ref="channelRankRef" />
      </el-tab-pane>
    </el-tabs>
  </el-col>
</template>
<script lang="ts" setup>
import CallRank from './CallRank.vue'
import ConnectRank from './ConnectRank.vue'
import DurationRank from './DurationRank.vue'
import ChannelRank from './ChannelRank.vue'
import { defaultProps, handleTree } from '@/utils/tree'
import * as DeptApi from '@/api/system/dept'
import { beginOfDay, defaultShortcuts, endOfDay, formatDate } from '@/utils/formatTime'
import { useUserStore } from '@/store/modules/user'
import dayjs from 'dayjs'

defineOptions({ name: 'CrmStatisticsRank' })

// 今天时间范围
const getTodayTimeRange = () => {
  const start = dayjs().startOf('day') // 今天的开始时间
  const end = dayjs().endOf('day') // 今天的结束时间
  return [start.format('YYYY-MM-DD HH:mm:ss'), end.format('YYYY-MM-DD HH:mm:ss')]
}
const queryParams = reactive({
  deptId: useUserStore().getUser.deptId,
  times: getTodayTimeRange()
})

const queryFormRef = ref() // 搜索的表单
const deptList = ref<Tree[]>([]) // 树形结构
const activeTab = ref('callRank')
const callRankRef = ref() // ContractPriceRank 组件的引用
const connectRankRef = ref() // ReceivablePriceRank 组件的引用
const durationRankRef = ref() // ContractCountRank 组件的引用
const channelRankRef = ref() // ContractCountRank 组件的引用

/** 搜索按钮操作 */
const handleQuery = () => {
  switch (activeTab.value) {
    case 'callRank':
      callRankRef.value?.loadData?.()
      break
    case 'connectRank':
      connectRankRef.value?.loadData?.()
      break
    case 'durationRank':
      durationRankRef.value?.loadData?.()
      break
    case 'channelRank':
      channelRankRef.value?.loadData?.()
      break
  }
}

// 当 activeTab 改变时，刷新当前活动的 tab
watch(activeTab, () => {
  handleQuery()
})

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  queryParams.times = getTodayTimeRange()
  handleQuery()
}

// 加载部门树
onMounted(async () => {
  deptList.value = handleTree(await DeptApi.getSimpleDeptList())
})
</script>
<style lang="scss" scoped></style>
